from django.contrib import admin
from .models import StockAlert, StockMovement, Supplier, PurchaseOrder, PurchaseOrderItem, GlobalStockSettings


class StockMovementInline(admin.TabularInline):
    model = StockMovement
    extra = 0
    readonly_fields = ['movement_type', 'quantity', 'previous_quantity', 'new_quantity', 'created_at']
    can_delete = False


@admin.register(GlobalStockSettings)
class GlobalStockSettingsAdmin(admin.ModelAdmin):
    list_display = ['default_min_stock', 'default_max_stock', 'auto_reorder', 'created_at']
    
    def has_add_permission(self, request):
        # 只允许存在一个全局设置
        if self.model.objects.exists():
            return False
        return super().has_add_permission(request)


@admin.register(StockAlert)
class StockAlertAdmin(admin.ModelAdmin):
    list_display = ['book', 'alert_type', 'current_stock', 'threshold', 'is_resolved', 'created_at']
    list_filter = ['alert_type', 'is_resolved', 'created_at']
    search_fields = ['book__title', 'book__isbn']
    readonly_fields = ['book', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('book', 'alert_type', 'current_stock', 'threshold')
        }),
        ('状态', {
            'fields': ('is_resolved', 'resolved_at', 'resolved_by')
        }),
        ('时间', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        })
    )


@admin.register(StockMovement)
class StockMovementAdmin(admin.ModelAdmin):
    list_display = ['book', 'movement_type', 'quantity', 'previous_quantity', 'new_quantity', 'created_at']
    list_filter = ['movement_type', 'created_at']
    search_fields = ['book__title', 'book__isbn', 'reference_number']
    readonly_fields = ['book', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('book', 'movement_type', 'quantity', 'previous_quantity', 'new_quantity')
        }),
        ('参考信息', {
            'fields': ('reference_number', 'note'),
            'classes': ('collapse',)
        }),
        ('时间', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        })
    )


@admin.register(Supplier)
class SupplierAdmin(admin.ModelAdmin):
    list_display = ['name', 'contact_person', 'phone', 'email', 'is_active', 'created_at']
    list_filter = ['is_active', 'created_at']
    search_fields = ['name', 'contact_person', 'email', 'phone']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'contact_person', 'phone', 'email')
        }),
        ('地址', {
            'fields': ('address', 'city', 'province', 'postal_code'),
            'classes': ('collapse',)
        }),
        ('状态', {
            'fields': ('is_active', 'notes')
        })
    )


class PurchaseOrderItemInline(admin.TabularInline):
    model = PurchaseOrderItem
    extra = 1
    fields = ['book', 'quantity', 'unit_price', 'subtotal']


@admin.register(PurchaseOrder)
class PurchaseOrderAdmin(admin.ModelAdmin):
    list_display = ['order_number', 'supplier', 'total_amount', 'status', 'expected_date', 'created_at']
    list_filter = ['status', 'created_at']
    search_fields = ['order_number', 'supplier__name']
    readonly_fields = ['order_number', 'created_at']
    inlines = [PurchaseOrderItemInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('order_number', 'supplier', 'status')
        }),
        ('金额信息', {
            'fields': ('total_amount', 'tax_amount', 'shipping_cost')
        }),
        ('时间信息', {
            'fields': ('order_date', 'expected_date', 'received_date')
        }),
        ('备注', {
            'fields': ('notes',),
            'classes': ('collapse',)
        })
    )


@admin.register(PurchaseOrderItem)
class PurchaseOrderItemAdmin(admin.ModelAdmin):
    list_display = ['purchase_order', 'book', 'quantity', 'unit_price', 'subtotal']
    search_fields = ['purchase_order__order_number', 'book__title']
    readonly_fields = ['purchase_order', 'book', 'subtotal']
